package services.common;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.common.entity.t_finance_record;
import models.common.entity.t_finance_record.IsPayment;
import services.base.BaseService;

import common.enums.ServiceType;
import common.utils.Factory;
import common.utils.OrderNoUtil;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.FinanceRecordDao;


/**
 * 大数据智能分析报告记录的业务逻辑层
 *@author LiangQiongfeng
 *@version 2018-1-20 下午4:09:57
 */
public class FinanceRecordService extends BaseService<t_finance_record> {
	
	protected static FinanceRecordDao financeRecordDao = Factory.getDao(FinanceRecordDao.class);

	/**
	 *	新增大数据智能分析报告记录
	 * @param leaderId 放款人id
	 * @param credit_application_id 授信申请人id
	 * @param fileName
	 * @param url
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-20 
	 */
	public ResultInfo addRecord(long leaderId, long credit_application_id, String fileName, String url) {
		
		ResultInfo result = new ResultInfo();
		
		t_finance_record record =  new t_finance_record();
		
		record.file_name = fileName;
		record.file_url = url;
		record.time = new Date();
		record.supervisor_id = leaderId;
		record.credit_application_id = credit_application_id;
		record.is_payment = t_finance_record.IsPayment.IS_PAY.code;
		
		boolean flag = financeRecordDao.save(record);
		if(!flag){
			result.code = -1;
			result.msg = "大数据智能分析报告购买失败！";
			return result;
					
		}
		result.code = 1;
		result.msg = "购买大数据智能分析报告成功！";
		return result;
	}

	/**
	 *	根据放款人和授信申请人id查询所购买的大数据智能分析报告
	 * @param leaderId 放款人id
	 * @param creditId 授信申请人id
	 * @param currPage 
	 * @param pageSize 
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-21 
	 */
	public PageBean<t_finance_record> findPage(long leaderId, long creditId, int currPage, int pageSize) {
		
		return financeRecordDao.findPage(leaderId,creditId,currPage,pageSize);
	}

	/**
	 *	上传文件后更新大数据智能分析报告记录
	 * @param leaderId 放款人id 
	 * @param companyId	授信申请人id
	 * @param fileName 文件名
	 * @param url 文件下载路径
	 * @author LiangQiongfeng
	 *@version 2018-1-23 
	 */
	public ResultInfo updateRecord(long leaderId, long companyId, String fileName, String url) {
		
		return financeRecordDao.updateRecord(leaderId, companyId,fileName, url);
	}

	
	/**
	 *	购买一次只能上传一次资料
	 * @param userId 用户id 
	 * @param companyId 企业id
	 * @param status 状态
	 * @param id 分析报告记录id
	 * @author Liangqiongfeng
	 *@version 2018-2-8 
	 */
	public int updateRecordByOderNo(long id,long userId, long companyId, int status,int oldStatus) {
		StringBuffer updateSQL = new StringBuffer("UPDATE t_finance_record SET is_payment =:is_payment WHERE 1 = 1");
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("is_payment", status);
		if(id > 0 ){
			updateSQL.append(" AND id = :id");
			condition.put("id", id);
		}
		if(userId > 0){
			updateSQL.append(" AND supervisor_id = :supervisor_id");
			condition.put("supervisor_id", userId);
		}
		if(companyId > 0){
			updateSQL.append(" AND credit_application_id = :credit_application_id");
			condition.put("credit_application_id", companyId);
		}
		updateSQL.append(" AND is_payment = :old_is_payment");
		condition.put("old_is_payment", oldStatus);
		
		return financeRecordDao.updateBySQL(updateSQL.toString(), condition);
	}

	/**
	 *	根据放款人和授信申请人id查询财务信息记录
	 * @param leaderId
	 * @param companyId
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-02-08
	 */
	public List<t_finance_record> findListById(long leaderId, long companyId) {
		StringBuffer querySQL = new StringBuffer("SELECT id,file_name,file_url,time,is_payment,credit_application_id,supervisor_id FROM t_finance_record WHERE 1 = 1");
		Map<String,Object> condition = new HashMap<String,Object>();
		if(leaderId > 0 && companyId > 0){
			querySQL.append(" AND credit_application_id = :credit_application_id  AND supervisor_id = :supervisor_id");
			condition.put("credit_application_id", companyId);
			condition.put("supervisor_id", leaderId);
		}
		return financeRecordDao.findListBySQL(querySQL.toString(), condition);
	}

}
